package com.example.litehdfs.config;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.fasterxml.jackson.annotation.JsonFormat;

import lombok.Data;

/**
 * 分页类
 * 
 * @author mading
 *
 * @param <T>
 */
@Data
public class PageInfo<T> implements Serializable {

	private static final long serialVersionUID = 1L;

	private Long totalCount;// 总记录数

	private Long totalPages;// 总页数

	private int pageNumber = 1;// 当前页数

	private int pageSize = 20;// 分页大小

	@JsonFormat(with = JsonFormat.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
	private List<T> list = new ArrayList<>();// 分页数据

	public PageInfo() {
	}

	public PageInfo(Integer pageSize, Integer pageNumber) {
		if(pageSize != null){
			this.pageSize = pageSize;
		}
		if(pageNumber != null){
			this.pageNumber = pageNumber;
		}
		
	}

	public PageInfo(Integer pageSize, Integer pageNumber, List<T> list) {
		if(pageSize != null){
			this.pageSize = pageSize;
		}
		if(pageNumber != null){
			this.pageNumber = pageNumber;
		}
		this.list = list != null ? list : new ArrayList<>();
	}

	public Long getTotalPages() {
		if (this.totalCount == null || this.totalCount == 0) {
			return 0L;
		}

		if (totalPages == null) {
			return (this.totalCount + this.pageSize - 1) / this.pageSize;
		}
		return totalPages;
	}

}
